package ro.gateway.aida.db;

import java.io.IOException;
import java.sql.Connection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;

import javax.servlet.ServletContext;

import ro.gateway.aida.servlet.Constants;
import ro.gateway.aida.srv.ConfigurationException;
import ro.gateway.aida.srv.ICFService;
import ro.gateway.aida.srv.IConfigurator;
import ro.gateway.aida.srv.PropertiesConfigurator;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: CFServiceBase.java,v 1.2 2005/04/04 00:09:02 mihaipostelnicu Exp $
 */
public class CFServiceBase implements ICFService
{
      public int status = NOT_CONFIGURED;

      Hashtable pools;

      public CFServiceBase()
      {
            pools = new Hashtable();
      }

      public Connection getConnection( String poolName )
      {
            IDBPool pool = ( IDBPool ) pools.get( poolName );
            if ( pool == null )
            {
                  System.err.println( "Pool:" + poolName + " doesn't exist" );
                  return null;
            }

            Connection con = pool.getConnection();
            return con;
      }

      public void reconfigure( IConfigurator configurator )
            throws ConfigurationException
      {
            if ( configurator instanceof PropertiesConfigurator )
            {
                  configure( ( PropertiesConfigurator ) configurator );
            }
            else
            {
                  throw new RuntimeException( "unimplemented" );
            }
      }

      /**
       * Suporta atribute de forma:
       * pool_name.driver
       * pool_name.url
       * pool_name.username
       * pool_name.password
       *
       * @param configurator
       */
      public void configure( ServletContext sc )
            throws IOException
      {
            DBConnectorProperties dbc=new DBConnectorProperties(sc);
            dbc.loadConnectors();
            
            Constants.DBS1=dbc.getCurrent();
            Constants.DBC1=dbc.getConnector(dbc.getCurrent());
            
            Iterator i =dbc.getConnectors().iterator();
            while (i.hasNext()) {
                DBConnector c = (DBConnector) i.next();
                pools.put( c.getName(), new SimpleDBPool(c) );    
                
            }
            
            
      }

      /**
       * @param configurator
       * 
       */
      private void configure( PropertiesConfigurator configurator )
            throws ConfigurationException
      {
            status = CONFIGURING;
            
            //configuring the default database
            Constants.DBS1=configurator.properties.getProperty("current");

            Enumeration en = configurator.properties.propertyNames();
            while ( en.hasMoreElements() )
            {
                  String name = ( String ) en.nextElement();
                  int point_index = name.indexOf( '.' );
                  if ( point_index > 0 )
                  {
                        String poolName = name.substring( 0, point_index );
                        if ( pools.get( poolName ) == null )
                        {
                              // nu a mai fost introdus
                              pools.put( poolName, new SimpleDBPool(null) );
                        }
                  }
            } // property iterator

            en = pools.keys();
            while ( en.hasMoreElements() )
            {
                  String name = ( String ) en.nextElement();

                  IDBPool dbPool = ( IDBPool ) pools.get( name );
                  PropertiesConfigurator poolConfigurator =
                        new PropertiesConfigurator();
                  poolConfigurator.properties.setProperty(
                        "db_driver", configurator.properties.getProperty( name + ".driver" ) );
                  poolConfigurator.properties.setProperty(
                        "db_url", configurator.properties.getProperty( name + ".url" ) );
                  poolConfigurator.properties.setProperty(
                        "db_username", configurator.properties.getProperty( name + ".username" ) );
                  poolConfigurator.properties.setProperty(
                        "db_password", configurator.properties.getProperty( name + ".password" ) );

                  try
                  {
                        dbPool.configure( poolConfigurator );
                     //   System.out.println(dbPool)
                  }
                  catch ( ConfigurationException cfgEx )
                  {
                       java.util.Iterator en1 = pools.values().iterator();
                        while ( en1.hasNext() )
                        {
                              IDBPool pool = ( IDBPool ) en1.next();
                              pool.release();
                        }

                        status = NOT_CONFIGURED;
                        throw cfgEx;
                        
                  }
            }

            status = CONFIGURED;
            System.out.println( "CFServiceBase configured properly" );
      }

      public static final int NOT_CONFIGURED = 0;
      public static final int CONFIGURED = 0;
      public static final int CONFIGURING = 0;
}